package com.ccp.dev.workflow.model;

import com.ccp.dev.core.basic.model.BaseModel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.TimeUtil;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.model.bpm.ProcessTask;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.delegate.DelegateTask;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 对象功能:流程任务审批意见Model对象 开发人员:csx 创建时间:2012-01-11 16:06:10
 */
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class TaskOpinion extends BaseModel {
    /**
     * 初始化\尚未审批=-2
     */
    public final static Short STATUS_INIT = (short) -2;
    /**
     * 正在审批
     */
    public final static Short STATUS_CHECKING = (short) -1;
    /**
     * 弃权=0
     */
    public final static Short STATUS_ABANDON = (short) 0;
    /**
     * 同意=1
     */
    public final static Short STATUS_AGREE = (short) 1;
    /**
     * 反对=2
     */
    public final static Short STATUS_REFUSE = (short) 2;
    /**
     * 驳回=3
     */
    public final static Short STATUS_REJECT = (short) 3;
    /**
     * 撤销=4
     */
    public final static Short STATUS_RECOVER = (short) 4;

    /**
     * 会签通过
     */
    public final static Short STATUS_PASSED = (short) 5;

    /**
     * 会签不通过。
     */
    public final static Short STATUS_NOT_PASSED = (short) 6;

    /**
     * 知会意见。
     */
    public final static Short STATUS_NOTIFY = (short) 7;

    /**
     * 更改执行路径
     */
    public final static Short STATUS_CHANGEPATH = (short) 8;

    /**
     * 终止
     */
    public final static Short STATUS_ENDPROCESS=(short)14;

    /**
     * 沟通意见。
     */
    public final static Short STATUS_COMMUNICATION=(short)15;

    /**
     * 沟通反馈。
     */
    public final static Short STATUS_COMMUN_FEEDBACK=(short)20;

    /**
     * 办结转发
     */
    public final static Short STATUS_FINISHDIVERT=(short)16;

    /**
     * 转办。
     */
    public final   static Short STATUS_DELEGATE=(short)21;

    /**
     * 转办取消。
     */
    public final static Short STATUS_DELEGATE_CANCEL=(short)22;

    /**
     * 更改执行人。
     */
    public final static Short STATUS_CHANGE_ASIGNEE = (short) 23;
    /**
     * 驳回到发起人
     */
    public final static Short STATUS_REJECT_TOSTART = (short) 24;
    /**
     * 撤销到发起人
     */
    public final static Short STATUS_RECOVER_TOSTART = (short) 25;



    /**
     * 撤销
     */
    public final static Short STATUS_REVOKED=(short)17;

    /**
     * 逻辑删除
     */
    public final static Short STATUS_DELETE=(short)18;

    /**
     * 抄送
     */
    public final static Short STATUS_NOTIFY_COPY=(short)19;

    /**
     * 代理。
     */
    public final   static Short STATUS_AGENT=(short)26;
    /**
     * 代理撤消。
     */
    public final   static Short STATUS_AGENT_CANCEL=(short)27;

    /**
     * 表单意见。
     */
    public final   static Short STATUS_OPINION=(short)28;
    /**
     * 驳回取消
     */
    public final   static Short STATUS_BACK_CANCEL=(short)29;

    /**
     * 撤销取消
     */
    public final   static Short STATUS_REVOKED_CANCEL=(short)30;
    /**
     * 通过取消
     */
    public final   static Short STATUS_PASS_CANCEL=(short)31;
    /**
     * 拒绝取消
     */
    public final   static Short STATUS_REFUSE_CANCEL=(short)32;
    /**
     * 提交
     */
    public final   static Short STATUS_SUBMIT=(short)33;
    /**
     * 重新提交
     */
    public final   static Short STATUS_RESUBMIT=(short)34;

    /**
     * 干预
     */
    public final   static Short STATUS_INTERVENE=(short)35;

    /**
     * 重启任务
     */
    public final   static Short STATUS_RESTART_TASK=(short)36;

    /**
     * 执行过，用于自动节点记录状态。
     */
    public final static Short STATUS_EXECUTED=(short)37;

    /**
     * 抄送通知
     */
    public final static String TASKKEY_NOTIFY="NotifyTask";
    /**
     * 办结转发
     */
    public final static String TASKKEY_DIVERT="DivertTask";
    /**表示已读 =1*/
    public final static Short READ = 1;
    /**表示未读=0*/
    public final static Short NOT_READ = 0;
    /**
     * 流转
     */
    public final static Short STATUS_TRANSTO=(short)38;
    /**
     * 流转中
     */
    public final static Short STATUS_TRANSTO_ING=(short)39;
    /**
     * 代提交
     */
    public final static Short STATUS_REPLACE_SUBMIT=(short)40;
    /**
     * 正常流转，即无别人干预
     */
    public final static Short STATUS_COMMON_TRANSTO=(short)41;
    /**
     * 流转取消
     */
    public final static Short STATUS_CANCLE_TRANSTO=(short)42;
    /**
     * 添加流转人
     */
    public final static Short STATUS_ADD_TRANSTO=(short)43;
    /**
     * 补签
     */
    public final static Short STATUS_RETROACTIVE=(short)44;
    /**
     * 追回
     */
    public final static Short STATUS_RETRIEVE=(short)45;

    /**
     * 交接
     */
    public final   static Short STATUS_HANDOVER=(short)46;

    // opinionId
    protected String opinionId;
    // procInstId
    protected String procInstId;
    // 任务名称
    protected String taskName;
    // 任务Key
    protected String taskKey;
    // 任务令牌
    protected String taskToken;
    // taskId
    protected String taskId;
    // 执行开始时间
    protected java.util.Date startTime;
    // 结束时间
    protected java.util.Date endTime;
    // 持续时间
    protected Long durTime;
    // 执行人ID
    protected String exeUserId;
    // 执行人名
    protected String exeFullname;
    // 审批意见
    protected String opinion;
    //审批附件
    protected String attachment;

    // 审批状态
    protected Short checkStatus = STATUS_CHECKING;
    // 表单定义Id
    protected String formDefId ;
    // 意见表单名称
    protected String fieldName;
    // 流程定义ID
    protected String procDefId;
    // 父流程实例ID
    protected String superExecution;


    protected String candidateUser="";

    private List<SysUser> candidateUsers=new ArrayList<SysUser>();

    public List<TaskExeStatus> candidateUserStatusList=new ArrayList<TaskExeStatus>();

    public TaskExeStatus taskExeStatus;

    protected String processName;


    /**
     * 抄送表的中状态，0为未读，1为已读

     protected Short status=0;
     */
    protected String status;

    protected String startTimeStr;
    protected String endTimeStr;
    protected String durTimeStr;

    public TaskOpinion() {

    }

    public TaskOpinion(ProcessTask task) {
        this.procDefId = task.getProcessDefinitionId();
        this.procInstId = task.getProcessInstanceId();
        this.taskId = task.getId();
        this.taskName = task.getName();
        this.taskKey = task.getTaskDefinitionKey();
        this.startTime = new Date();
    }

    public TaskOpinion(DelegateTask task) {
        this.procDefId = task.getProcessDefinitionId();
        this.procInstId = task.getProcessInstanceId();
        this.taskId = task.getId();
        this.taskKey = task.getTaskDefinitionKey();
        this.taskName = task.getName();
        this.checkStatus=STATUS_CHECKING;
        this.startTime = new Date();
        ExecutionEntity executionEntity = CommandContextUtil.getExecutionEntityManager().findById(task.getProcessInstanceId()).getSuperExecution();
        if (executionEntity != null) {
            this.superExecution = executionEntity.getProcessInstanceId();
        }
    }

    public void setOpinionId(String opinionId) {
        this.opinionId = opinionId;
    }

    /**
     * 返回 opinionId
     *
     * @return
     */
    public String getOpinionId() {
        return opinionId;
    }

    public void setProcInstId(String procInstId) {
        this.procInstId = procInstId;
    }

    /**
     * 返回 actInstId
     *
     * @return
     */
    public String getProcInstId() {
        return procInstId;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    /**
     * 返回 任务名称
     *
     * @return
     */
    public String getTaskName() {
        return taskName;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    /**
     * 返回 taskId
     *
     * @return
     */
    public String getTaskId() {
        return taskId;
    }

    public void setStartTime(java.util.Date startTime) {
        this.startTime = startTime;
    }

    /**
     * 返回 执行开始时间
     *
     * @return
     */
    public java.util.Date getStartTime() {
        return startTime;
    }

    public String getStartTimeStr() {
        if (startTime == null)
        {return "";}
        return TimeUtil.getDateTimeString(this.startTime);
    }

    public void setEndTime(java.util.Date endTime) {
        this.endTime = endTime;
    }

    /**
     * 返回 结束时间
     *
     * @return
     */
    public java.util.Date getEndTime() {

        return endTime;
    }

    public String getEndTimeStr() {

        if (endTime == null)
        { return "";}
        return TimeUtil.getDateTimeString(this.endTime);
    }

    public void setDurTime(Long durTime) {
        this.durTime = durTime;
    }

    public String getDurTimeStr() {
        if (this.durTime == null)
        {return "";}
        return TimeUtil.getTime(this.durTime);
    }

    /**
     * 返回 持续时间
     *
     * @return
     */
    public Long getDurTime() {
        return durTime;
    }

    public void setExeUserId(String exeUserId) {
        this.exeUserId = exeUserId;
    }

    /**
     * 返回 执行人ID
     *
     * @return
     */
    public String getExeUserId() {
        return exeUserId;
    }

    public void setExeFullname(String exeFullname) {
        this.exeFullname = exeFullname;
    }

    /**
     * 返回 执行人名
     *
     * @return
     */
    public String getExeFullname() {
        if (StringUtil.isEmpty(exeFullname))
        {return "";}
        return exeFullname;
    }

    public void setOpinion(String opinion) {
        this.opinion = opinion;
    }

    /**
     * 返回 审批意见
     *
     * @return
     */
    public String getOpinion() {
        return opinion;
    }

    public String getAttachment() {
        return attachment;
    }

    public void setAttachment(String attachment) {
        this.attachment = attachment;
    }

    public void setCheckStatus(Short checkStatus) {
        this.checkStatus = checkStatus;
    }

   public String getStatus() {
        //获取流程状态
        return getTaskStatus(checkStatus, 1);
    }


    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * 返回 审批状态(来自数据字典)
     *
     * @return
     */
    public Short getCheckStatus() {
        return checkStatus;
    }

    public String getProcessName() {
        return processName;
    }

    public void setProcessName(String processName) {
        this.processName = processName;
    }

    /**
     * 获取表单定义ID
     *
     * @return
     */
    public String getFormDefId() {
        return formDefId;
    }

    public void setFormDefId(String formDefId) {
        this.formDefId = formDefId;
    }

    /**
     * 获取表单定义名称。
     *
     * @return
     */
    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    /**
     * 获取流程定义ID。
     *
     * @return
     */
    public String getProcDefId() {
        return procDefId;
    }

    public void setProcDefId(String procDefId) {
        this.procDefId = procDefId;
    }

    public String getTaskKey() {
        return taskKey;
    }

    public void setTaskKey(String taskKey) {
        this.taskKey = taskKey;
    }

    public String getTaskToken() {
        return taskToken;
    }

    public void setTaskToken(String taskToken) {
        this.taskToken = taskToken;
    }

    public String getSuperExecution() {
        return superExecution;
    }

    public void setSuperExecution(String superExecution) {
        this.superExecution = superExecution;
    }

    public String getCandidateUser() {
        return candidateUser;
    }

    public void setCandidateUser(String candidateUser) {
        this.candidateUser = candidateUser;
    }

    public void setCandidateUsers(List<SysUser> candidateUsers) {
        this.candidateUsers = candidateUsers;
    }

    public List<SysUser> getCandidateUsers() {
        return candidateUsers;
    }


    public List<TaskExeStatus> getCandidateUserStatusList() {
        return candidateUserStatusList;
    }

    public void setCandidateUserStatusList(List<TaskExeStatus> candidateUserStatusList) {
        this.candidateUserStatusList = candidateUserStatusList;
    }

    public TaskExeStatus getTaskExeStatus() {
        return taskExeStatus;
    }

    public void setTaskExeStatus(TaskExeStatus taskExeStatus) {
        this.taskExeStatus = taskExeStatus;
    }


    /**
     * @see java.lang.Object#equals(Object)
     */
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof TaskOpinion)) {
            return false;
        }
        TaskOpinion rhs = (TaskOpinion) object;
        return new EqualsBuilder().append(this.opinionId, rhs.opinionId).isEquals();
    }

    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder(-82280557, -700257973).append(this.opinionId).toHashCode();
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this).append("opinionId", this.opinionId).append("procInstId", this.procInstId)
                .append("taskName", this.taskName).append("taskId", this.taskId).append("startTime", this.startTime)
                .append("endTime", this.endTime).append("durTime", this.durTime).append("exeUserId", this.exeUserId)
                .append("exeFullname", this.exeFullname).append("opinion", this.opinion)
                .append("checkStatus", this.checkStatus).append("procDefId", this.procDefId)
                .append("superExecution", this.superExecution).toString();
    }
    /**
     * 获取流程状态
     *  yaoluyang
     * @param status
     * @param flag
     * @return 流程状态
     */
    public  String getTaskStatus(Short status, int flag) {
        switch (status) {
            case -2://尚未审批
                return "<font color='red'>尚未审批</font>";
            case -1://尚未处理(0) || 正在审批(1)
                return flag==0?"尚未处理":"<font color='red'>正在审批</font>";
            case 0://弃权
                return "<font color='red'>弃权</font>";
            case 1://同意
                return "<font color='green'>同意</font>";
            case 2://反对
                return "<font color='orange'>反对</font>";
            case 3://驳回
                return "<font color='red'>驳回</font>";
            case 4://撤销
                return "<font color='red'>撤销</font>";
            case 5://会签通过
                return "<font color='green'>会签通过</font>";
            case 6://会签不通过
                return "<font color='red'>会签不通过</font>";
            case 7://知会意见
                return "<font color='red'>知会意见</font>";
            case 8://更改执行路径
                return "<font color='red'>更改执行路径</font>";
            case 9://终止
                return "<font color='red'>终止</font>";
            case 10://沟通意见
                return "<font color='red'>沟通意见</font>";
            case 14://终止
                return "<font color='red'>终止</font>";
            case 15://沟通
                return "<font color='red'>沟通 </font>";
            case 16://办结转发
                return "<font color='orange'>办结转发</font>";
            case 17://撤销
                return "<font color='orange'>撤销</font>";
            case 18://删除
                return "<font color='orange'>删除</font>";
            case 19://抄送
                return "<font color='orange'>抄送</font>";
            case 20://沟通反馈
                return "<font color='green'>沟通反馈</font>";
            case 21://转办
                return "<font color='red'>转办</font>";
            case 22://取消转办
                return "<font color='red'>取消转办</font>";
            case 23://更改执行人
                return "<font color='red'>更改执行人</font>";
            case 24://驳回到发起人
                return "<font color='red'>驳回到发起人</font>";
            case 25://撤销(撤销到发起人)
                return "<font color='red'>撤销(撤销到发起人)</font>";
            case 26://代理
                return "<font color='brown'>代理</font>";
            case 27://取消代理
                return "<font color='green'>取消代理</font>";
            case 28://表单意见
                return "<font color='green'>表单意见</font>";
            case 29://表单意见
                return "<font color='green'>驳回取消</font>";
            case 30://撤销取消
                return "<font class='brown'>撤销取消</font>";
            case 31://通过取消
                return "<font class='brown'>通过取消</font>";
            case 32://反对取消
                return "<font class='brown'>反对取消</font>";
            case 33://提交
                return "<font class='green'>提交</font>";
            case 34://重新提交
                return "<font class='green'>重新提交</font>";
            case 35://干预
                return "<font class='brown'>干预</font>";
            case 36://重启任务
                return "<font class='brown'>重启任务</font>";
            case 38://流转
                return "<font color='green'>流转</font>";
            case 40://代提交
                return "<font color='red'>代提交</font>";
            case 42://取消流转
                return "<font color='red'>取消流转</font>";
            case 43://添加流转人
                return "<font color='red'>添加流转人</font>";
            case 44://补签
                return "<font color='red'>补签</font>";
            case 45://追回
                return "<font color='red'>追回</font>";
            default:
                break;
        }
        return "";
    }
}
